package com.slmn.visitor.gui;

import com.slmn.visitor.gui.Component.CNodeState;
import com.slmn.visitor.gui.Component.HChild;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Bounds;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

public class Cursor extends Rectangle {
	final static private Timeline caretTimeline = new Timeline();
	//Only one cursor on screen;
	final public static Cursor prompt = new Cursor();
	private static HChild cNode;

	private Cursor() {
		super(0, 0, 1.6, 18);
		this.setFill(Color.BLACK);
		caretTimeline.setCycleCount(Timeline.INDEFINITE);
		caretTimeline.getKeyFrames().addAll(
				new KeyFrame(Duration.ZERO, event -> {
					this.setFill(Color.TRANSPARENT);
				}), new KeyFrame(Duration.seconds(.5), event -> {
					Cursor.this.setFill(Color.BLACK);
				}), new KeyFrame(Duration.seconds(1)));
		caretTimeline.play();
		this.setCursor(javafx.scene.Cursor.TEXT);
		// this.setOnMouseClicked(event -> {
		// System.out.println(this.getLayoutBounds());
		// });
	}
	
	public static void clearCursor(){
		cNode.setCNodesBG(CNodeState.CLEAR);
		Cursor.prompt.setVisible(false);
	}

	public static void setCursor(Component c) {
		if (!(c instanceof HChild)) {
			return;
		}
		HChild hc = (HChild) c;
		Bounds b = hc.getCBounds();
		hc.setCNodesBG(CNodeState.FOCUSED);
		cNode = hc;
		Cursor.prompt.setHeight(b.getHeight());
//		Cursor.prompt.relocate(b.getMinX() + b.getWidth(), b.getMinY());
		Cursor.prompt.setVisible(true);
		hc.addCursor();
	}
}

